//
//  LocalPlayer.m
//  Maze1
//
//  Created by Lin Tian on 10/21/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "LocalPlayer.h"
#import "Room.h"

@implementation LocalPlayer

@synthesize viewDirection = _viewDirection;
@synthesize viewPort = _viewPort;
@synthesize viewRadius = _viewRadius;
@synthesize name = _name;
@synthesize pwd = _pwd;
@synthesize deltaSpeed = _deltaSpped;

/*
 *used when local player login into the game
 *
 */
+(id) playerWithPlayerName:(NSString *)playerName Pwd:(NSString*)pwd
{
    return[[[self alloc] initWithPlayerName:playerName Pwd:pwd] autorelease];
}

/*
 *override methond in super class to add view to local player
 */
-(id) initSpriteWithParentNode:(CCNode*)parentNode position:(CGPoint)loc
{
	if ((self = [super init])) {
		_playerSprite = [CCSprite spriteWithFile:@"Player.png"];
		_playerSprite.position = loc;
		[parentNode addChild:_playerSprite];
        self.deltaSpeed = 8;
		
        self.position = loc;
        self.speed = CGPointMake(0, 0);
        _viewDirection = CGPointMake(1, 1);
        _viewRadius = VIEWRADIUS;
        _viewPort = VIEWPORT;
        
        /*CCSprite* view = [CCSprite spriteWithFile:@"view.png"];
         view.position = CGPointMake(40, view.contentSize.height/2);
         view.rotation = ccpAngle(ccp(1,1), ccp(0,1))*(180.0/M_PI);
         [self.playerSprite addChild:view z:2 tag:VIEWTAG];*/
    }
    return self;
}

/*
 *init method used when local player login into the game
 */
-(id) initWithPlayerName:(NSString*)name Pwd:(NSString*)pwd
{
    if ((self = [super init])) {
        self.name = name;
        self.pwd = pwd;
        self.loadingProgress = 0;
    }
    return self;
}

-(void)dealloc
{
    _name = nil;
    _pwd = nil;
    [super dealloc];
}

/*
 *used when local player create a room
 */
-(void)creatRoomWithMapId:(unsigned char)map numForces:(unsigned char)num
{
    self.room = [[Room alloc] initWithOwner:self mapId:map numForces:num];
}

/*
 *override method in super class. set the position and update 
 *position of local player's view
 */
-(void)setPosition:(CGPoint)position
{
    //send the position info to server
    
    //update the local position
    _position = position;
    _playerSprite.position = _position;
    
    /*CCSprite *footprint = [CCSprite spriteWithFile:@"footprint.gif"];
    footprint.position = _position;
    footprint.opacity = 100;
    [footprint runAction:[CCSequence actions:[CCFadeTo actionWithDuration:20 opacity: 0.0], [CCCallFuncN actionWithTarget:self selector:@selector(removeFootPrint:)], nil]];
    [self.playerSprite.parent addChild:footprint];*/
    
    /********************* need also change the view position*******/
}

/*
 *used when player pick up an item
 */
-(BOOL)pickupItem:(Item*)anItem
{
    if([anItem initSpriteWithParentNode:[self.playerSprite.parent.parent getChildByTag:HudTag] inPosition:CGPointMake(300, 300)])
    {
        [self.itemList addObject:anItem];
        return YES;
    }
    return NO;
}

/*-(void)useItem:(int)itemId
{
    Item* anItem = nil;
    for (int i = 0; i < [self.itemList count]; i++) {
        anItem = (Item*)[self.itemList objectAtIndex:i];
        if(anItem.idNum == itemId)
        {
            [anItem useItem];
        }
    }
}*/

@end
